﻿namespace OleDbDAL.Processing
{
    using DBUtility;
    using IDal.Processing;
    using Model.Processing;
    using System;
    using System.Data;
    using System.Text;

    public class TeaPackagingCommApp : ITeaPackagingComm
    {
        private string dbname = "Systems";

        public void Add(TeaPackagingCommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_TeaPackagingComm(");
            builder.Append("PackagingId,StockId,CommId,CommName,CommUnit,Weight");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.PackagingId + ",");
            builder.Append("" + model.StockId + ",");
            builder.Append("" + model.CommId + ",");
            builder.Append("'" + model.CommName + "',");
            builder.Append("" + model.CommUnit + ",");
            builder.Append("" + model.Weight + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int PackagingCommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_TeaPackagingComm ");
            builder.Append(" where PackagingCommId=" + PackagingCommId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_TeaPackagingComm ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by PackagingCommId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "PackagingCommId", "T_TeaPackagingComm", swhere);
        }

        public TeaPackagingCommEnt GetModel(int PackagingCommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select PackagingCommId,PackagingId,StockId,CommId,CommName,CommUnit,Weight from T_TeaPackagingComm ");
            builder.Append(" where PackagingCommId=" + PackagingCommId);
            TeaPackagingCommEnt ent = new TeaPackagingCommEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.PackagingCommId = PackagingCommId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["PackagingId"].ToString() != "")
                {
                    ent.PackagingId = int.Parse(table.Rows[0]["PackagingId"].ToString());
                }
                if (table.Rows[0]["StockId"].ToString() != "")
                {
                    ent.StockId = int.Parse(table.Rows[0]["StockId"].ToString());
                }
                if (table.Rows[0]["CommId"].ToString() != "")
                {
                    ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                }
                ent.CommName = table.Rows[0]["CommName"].ToString();
                if (table.Rows[0]["CommUnit"].ToString() != "")
                {
                    ent.CommUnit = int.Parse(table.Rows[0]["CommUnit"].ToString());
                }
                if (table.Rows[0]["Weight"].ToString() != "")
                {
                    ent.Weight = decimal.Parse(table.Rows[0]["Weight"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_TeaPackagingComm ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "PackagingCommId");
        }

        public void Update(TeaPackagingCommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_TeaPackagingComm set ");
            builder.Append("PackagingId=" + model.PackagingId + ",");
            builder.Append("StockId=" + model.StockId + ",");
            builder.Append("CommId=" + model.CommId + ",");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("CommUnit=" + model.CommUnit + ",");
            builder.Append("Weight=" + model.Weight + "");
            builder.Append(" where PackagingCommId=" + model.PackagingCommId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

